<template>
  <div class="dashboard-container">
    <div class="app-container">
      <PageTools>
        <span>本月:发放双倍奖金</span>
        <template #right>
          <el-button type="warning" size="small" @click="$router.push('/import')">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small" icon="plus" @click="showDialog=true">新增员工</el-button>
        </template>
      </PageTools>

      <el-card style="margin-top: 10px;">
        <el-table border :data="list">
          <el-table-column label="序号" type="index" sortable="" />
          <el-table-column label="姓名" prop="username" sortable="" />
          <el-table-column label="工号" prop="workNumber" sortable="" />
          <el-table-column label="聘用形式" sortable="">
            <template v-slot="{row}">
              {{ formHiretype(row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" sortable="" />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable="">
            <template v-slot="{row}">
              {{ row.timeOfEntry | formatTime }}
            </template>
          </el-table-column>
          <el-table-column label="账户状态" prop="enableState" sortable="">
            <template v-slot="{row}">
              <el-tooltip :content="'Switch value: ' + value" placement="top">
                <el-switch :inactive-value="0" :active-value="1" :value="row.enableState" active-color="green" inactive-color="lightgray" />
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template #default="{row}">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small">角色</el-button>
              <el-button type="text" size="small" @click="handleDelete(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <div style="height: 60px; margin-top: 10px">
          <el-pagination :total="total" layout="->,prev, pager, next,total" :current-page="pageParams.page" :page-size="pageParams.size" @current-change="handleCurrentChange" />
        </div>
      </el-card>
    </div>
    <!-- 添加员工组件 -->
    <add-employee :show-dialog.sync="showDialog" />

  </div>
</template>

<script>
import { reqGetEmployeeList, reqDelEmployee } from '@/api/employees'
import Types from '@/api/constant/employees'
import AddEmployee from './components/AddEmployee.vue'
export default {
  name: 'Employees',
  components: {
    AddEmployee
  },
  data () {
    return {
      showDialog: false,
      value: 1,
      //  聘用形式
      hireType: Types.hireType,
      // 员工列表
      list: [],
      // 总数
      total: 0,
      pageParams: {
        page: 1,
        size: 6
      }
    }
  },
  created () {
    this.loadEmployeeList()
  },
  methods: {
    // 删除员工方法二
    async handleDelete (id) {
      const ret = await this.$confirm('确认删除此员工吗？ 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 点击取消
      if (ret === 'cancel') return
      try {
        const ret = await reqDelEmployee(id)
        if (ret.code === 10000) {
          this.loadEmployeeList()
          this.$message.success('删除成功')
        }
        this.$message.success(ret.message)
      } catch {
        this.$message.error('删除失败')
      }
    },
    // 删除员工方法一
    // handleDelete (id) {
    //   this.$confirm('确认删除此员工吗？ 是否继续?', '提示', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   }).then(async () => {
    //     // 调用接口删除员工
    //     const ret = await reqDelEmployee(id)
    //     if (ret.code === 10000) {
    //       // 删除成功，刷新列表，提示成功
    //       this.loadEmployeeList()
    //       this.$message.success(ret.message)
    //     } else {
    //       // 删除失败
    //       this.$message.error(ret.message)
    //     }
    //   }).catch((err) => {
    //     if (err !== 'cancel') {
    //       this.$message.error('删除失败')
    //     }
    //   })
    // },
    // 格式化聘用形式
    formHiretype (type) {
      const info = this.hireType.find(item => parseInt(item.id) === parseInt(type))
      if (info) {
        return info.value
      } else {
        return '其他'
      }
    },
    // 监听页码的变化
    handleCurrentChange (index) {
      this.pageParams.page = index
      this.loadEmployeeList()
    },
    // 获取员工列表
    async loadEmployeeList () {
      try {
        const ret = await reqGetEmployeeList(this.pageParams)
        if (ret.code === 10000) {
          this.list = ret.data.rows
          this.total = ret.data.total
        }
      } catch {
        this.$message.error('获取员工列表失败')
      }
    }
  }

}
</script>

<style lang="scss" scoped>
</style>
